let alldata = []
let nowpage = 1;
let content = "`" +
  '<tr class="mwidth">' +
  '<td>' +
  '<input type="checkbox" id="" value="" class="box-checkbox">' +
  '</td>' +
  '<td>${data[i].商品编码}</td>' +
  '<td>' +
  '<img src="${data[i].商品信息[0].商品图片}">    ' +
  '<span>' +
  '<p>${data[i].商品信息[0].商品名}</p>' +
  '${data[i].商品信息[0].商品规格} ${data[i].商品信息[0].颜色} ${data[i].商品信息[0].尺寸} ${data[i].商品信息[0].款式}'
  + '<span>' +
  '</td>' +
  '<td>${data[i].当前售价}</td>' +
  '<td>${data[i].成本价}</td>' +
  '<td>${data[i].库存量}</td>' +
  '<td>${data[i].累计销量}</td>' +
  '<td>${data[i].日销量}</td>' +
  '<td>${data[i].评论时间}</td>' +
  '<td>${data[i].订单状态}</td>' +
  '<td>' +
  '<a onclick="order(this)">编辑 </a>  ' +
  '<a onclick="order(this)">上架 </a>  ' +
  '<a onclick="order(this)">复制 </a>  ' +
  '<a onclick="order(this)"> <br> <br>查看详情</span></a>  ' +
  '</td>' +
  '</tr>' + '`'
let pagecontent = "`" +
  "<li>" +
  "${i+1}" +
  "</li>"
  + "`"
let names = [
  {
    "name": '全部',
    "data": "all"
  },
  {
    "name": '在售中',
    "data": "在售中"
  },
  {
    "name": '已售空',
    "data": "已售空"
  },
  {
    "name": '已下架',
    "data": "已下架"
  },

]
// 导出
window.onload = function () {
  $('.daochub').on('click', function () {
    $('.daochu').modal('toggle')
  })
  // 搜索选项卡
  alldata = data
  for (let i = 0; i < names.length; i++) {
    checkshow(alldata, names[i].name, names[i].data)
  }
  // 分页
  $('.label-page').on('click', 'div', function () {
    check($(this), alldata, '.pagenum', ".mtable", parseInt($('#page_num').find("option:selected").val()), pagecontent, content)
  })
  // 从初始化
  init(alldata, '.pagenum', ".mtable", parseInt($('#page_num').find("option:selected").val()), pagecontent, content, 1)
  allcheck()
  // 分页
  $('#page_num').on('change', function () {
    init(alldata, '.pagenum', ".mtable", parseInt($(this).find("option:selected").val()), pagecontent, content, 1)
  })
  $('.reset').on('click', () => {
    let querys = $('.mcol').find('.minput');
    let x = parseInt($('#page_num').find("option:selected").val())
    let option;
    for (let i = 0; i < $('.label-page').find('div').length; i++) {
      if ($($('.label-page').find('div')[i]).attr('class') == 'hshow') {
        option = $($('.label-page').find('div')[i])
        break;
      }
    }
    for (let i = 0; i < querys.length; i++) {
      $(querys[i]).val('')
    }
    check(option, alldata, '.pagenum', ".mtable", x, pagecontent, content)
  })
  remove(alldata, '.pagenum', ".mtable", parseInt($('#page_num').find("option:selected").val()), pagecontent, content, 1)

}

function check(el, data, pagelocation, contentloaction, pagesnum, pagecontent, content) {
  for (let i = 0; i < el.parent().find('div').length; i++) {
    $(el.parent().find('div')[i]).removeClass('hshow')
  }
  el.addClass('hshow')
  if ($(el.find('span')[0]).text() != "全部") {
    let datas = data.filter(item => item.订单状态 == $(el.find('span')[0]).text())
    init(datas, pagelocation, contentloaction, pagesnum, pagecontent, content, 1)
  } else {
    init(data, pagelocation, contentloaction, pagesnum, pagecontent, content, 1)
  }
}

function init(data, pagelocation, contentloaction, pagesnum, pagecontent, content, nowpage) {
  $('.query').off('click')
  $('.query').on('click', function () {
    query(data, pagelocation, contentloaction, pagesnum, pagecontent, content, nowpage)
  })
  page(data, contentloaction, pagesnum, content, nowpage)
  pagenum(data, pagelocation, pagesnum, pagecontent)
  ingo(data, pagelocation, contentloaction, pagesnum, pagecontent, content)
  back(data, pagelocation, contentloaction, pagesnum, pagecontent, content)
  next(data, pagelocation, contentloaction, pagesnum, pagecontent, content)
}

function query(data, pagelocation, contentloaction, pagesnum, pagecontent, content, nowpage) {
  let querys = $('.mcol').find('.minput');
  let names = $('.mcol').find('span')
  let ns = []
  let values = [];
  let datas = data
  for (let i = 0; i < querys.length; i++) {
    values.push($(querys[i]).val());
    ns.push($(names[i]).text())
  }
  for (let i = 0; i < values.length; i++) {
    if (ns[i] == "评价时间") {
      values[i] = values[i].slice(0, 5) + "" + values[i].slice(5, 9) + "" + values[i].slice(9, 12)
    }
    if (values[i] != "请选择支付方式" && values[i] != "" && values[i] != null) {
      datas = datas.filter(item => item[ns[i]] == values[i])
    }
  }
  init(datas, pagelocation, contentloaction, pagesnum, pagecontent, content, nowpage)
}
// 删除
function remove(datas, pagelocation, contentloaction, pagesnum, pagecontent, content, nowpage) {
  let dels = []
  $('.del').on('click', function () {
    for (let i = 0; i < $('tbody>.mwidth>td').find('input').length; i++) {
      if ($($('tbody>.mwidth>td').find('input')[i]).prop('checked')) {
        dels.push($($($('tbody>.mwidth>td').find('input')[i]).parent().parent().find('td')[1]).text())
      }
    }
    for (let j = 0; j < dels.length; j++) {
      for (let i = 0; i < datas.length; i++) {
        if (datas[i].商品编码 == dels[j]) {
          datas.splice(i, 1)
        }
      }
    }
    for (let i = 0; i < $('.allcheck').length; i++) {
      $($('.allcheck')[i]).prop('checked', false)
    }
    $('.label-page').text('')
    for (let i = 0; i < names.length; i++) {
      checkshow(alldata, names[i].name, names[i].data)
    }
    init(datas, pagelocation, contentloaction, pagesnum, pagecontent, content, nowpage)
  })
}
function order(e) {
  let item = alldata.filter(item => item.订单号 == $($(e).parent().parent().find('td')[1]).text())
  sessionSet('msg', item[0])
  location.href = '../../html/shangpinguanli/xinzengshangpin2.html'
}